/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package chelydra.sharelydra.client.network;

/**
 *
 * @author Benjamin
 */
public class ByteConvertor {

    public static void main(String args[]){

        byte[] tab={3,-43,53,123,-34};
        String temp = toString(tab);
        System.out.println("temp : "+temp);
        System.out.println("fromString "+fromString(temp));
        System.out.println("orignal : "+tab + " new : "+ fromString(temp));
        System.out.println("");
        seeByteTab(fromString(temp));
     }

    public static void seeByteTab(byte[] b){
        for(int i=0;i < b.length ; i++){
            System.out.println(" case "+i+" = "+b[i]);
        }
    }

    public static String toString(byte[] tab){
        StringBuffer breply = new StringBuffer();
        for(int i=0;(i < tab.length) ; i++  ){
            breply.append("@");
            breply.append(Byte.toString(tab[i]));
        }
        breply.append("@");
        String reply = new String(breply);
        return reply;
    }

    public static byte[] fromString(String str){
        byte[] tabtemp = new byte[str.length()];
        int[] tabpos = new int[str.length()];
        int index = 0;
        int firstPosAt;
        int secondPosAt;
        String arobace;
        String strbis = new String(str);
        StringBuffer bstr = new StringBuffer(str);
        boolean goOn = true;
        int j=0;
        int pos1=strbis.indexOf("@");
        int pos2;
        while( pos1 !=-1 ){
            j++;
            pos2=strbis.indexOf("@",pos1+1);
            
            if(pos2 > pos1 ){
                arobace = bstr.substring(pos1+1,pos2);
                tabtemp[j-1]=Byte.parseByte(arobace);
                pos1 = pos2;
            }
            pos1 = pos2;
        }

        byte[] tab = new byte[j-1];
        for(int i=0;i < j-1 ; i++){
            tab[i]= tabtemp[i];
        }
        
        return tab;
    }
}
